package com.qf.web.controller;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * projectName:myxiaomi
 * author:jww
 * time:2024/4/15 15:02
 * description:
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    /**
     * 预览订单
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //http://localhost:8080/orderservlet?method=getOrderView
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //2调用业务逻辑
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //3获取地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(user.getId());
            //存储数据
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "预览订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    /**
     * 添加订单
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //orderservlet?method=addOrder&aid=7
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2获取前端数据
        String aid = request.getParameter("aid");
        //非空验证
        if (StringUtils.isEmpty(aid)) {
            request.setAttribute("msg", "地址不能为空");
            return "/message.jsp";
        }
        try {
            //3调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //3.1从购物车中获取money
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            if (cartList == null || cartList.size() == 0) {
                request.setAttribute("msg", "购物车不能为空");
                return "/message.jsp";
            }
            BigDecimal money = new BigDecimal(0);
            for (Cart cart : cartList) {
                money = money.add(cart.getMoney());
            }
            //创建一个订单号
            String oid = RandomUtils.creatOrderId();
            //状态 1未支付 2已支付未发货 3已发货待收货 4订单完成
            Order order = new Order(oid, user.getId(), money, "1", new Date(), Integer.parseInt(aid));
            orderService.submitOrder(order, cartList);//cartList
            request.setAttribute("order", order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    /**
     * 获取订单列表
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //orderservlet?method=getOrderList
    public String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //2调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            List<Order> orderList = orderService.find(user.getId());
            //存值
            request.setAttribute("orderList", orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    /**
     * 根据订单编号获取订单详情
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //orderservlet?method=getOrderDetail&oid202404181618483595793573
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2获取前端数据
        String oid = request.getParameter("oid");
        //非空验证
        if (StringUtils.isEmpty(oid)) {
            request.setAttribute("msg", "订单编号不能为空");
            return "/message.jsp";
        }
        try {
            //3调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //3.1查看订单
            Order order = orderService.findByOid(oid);
            //3.2订单详情
            List<OrderDetail> orderDetails = orderService.findDetail(oid);
            //存值
            request.setAttribute("order", order);
            request.setAttribute("orderDetails", orderDetails);
            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单详情失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //orderservlet?method=searchOrder?oid="+id;
    public String searchOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String status = request.getParameter("status");
        String id = request.getParameter("id");
        System.out.println("id="+id);
        System.out.println("status="+status);

        OrderService orderService=new OrderServiceImpl();
        List<Object> params =new ArrayList<>();
        StringBuilder where =new StringBuilder(" where 1=1 ");

        if(!StringUtils.isEmpty(id)){
            where.append(" and id=? ");
            params.add(id);
        }
        if(!StringUtils.isEmpty(status)){
            where.append(" and status=? ");
            params.add(status);
        }
        try {
            List<Order> orderList = orderService.fAll(where.toString(),params);
            request.setAttribute("orderList",orderList);
            return  "/admin/showAllOrder.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看管理失败："+e.getMessage());
            return "/message.jsp";
        }
    }
}

